/*
 * Copyright Thoughtworks, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.thoughtworks.go.apiv1.datefor

import com.thoughtworks.go.api.SecurityTestTrait
import com.thoughtworks.go.api.spring.ApiAuthenticationHelper
import com.thoughtworks.go.spark.AllowAllUsersSecurity
import com.thoughtworks.go.spark.ControllerTrait
import com.thoughtworks.go.spark.Routes
import com.thoughtworks.go.spark.SecurityServiceTrait
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test

class DateForControllerV1Test implements ControllerTrait<DateForControllerV1>, SecurityServiceTrait {

    @Override
    DateForControllerV1 createControllerInstance() {
        return new DateForControllerV1(new ApiAuthenticationHelper(securityService, goConfigService))
    }

    @Nested
    class Show {
        @Nested
        class Security implements SecurityTestTrait, AllowAllUsersSecurity {

            @Override
            String getControllerMethodUnderTest() {
                return "show"
            }

            @Override
            void makeHttpCall() {
                getWithApiHeader(controller.controllerPath() + "?num=4")
            }
        }

        @Test
        void 'should return date_for when num parameter is provided'() {
            getWithApiHeader(controller.controllerPath() + "?num=4")

            assertThatResponse()
                .isOk()
                .hasJsonBody([
                    "_links": [
                        "self": ["href": "http://test.host/go/api/date_for"]
                    ],
                    "date_for": "next_4"
                ])
                .hasContentType(controller.mimeType)
        }

        @Test
        void 'should return error when num parameter is missing'() {
            getWithApiHeader(controller.controllerPath())

            assertThatResponse()
                .isBadRequest()
                .hasJsonMessage("Missing required parameter 'num'")
        }

        @Test
        void 'should return error when num parameter is not an integer'() {
            getWithApiHeader(controller.controllerPath() + "?num=abc")

            assertThatResponse()
                .isBadRequest()
                .hasJsonMessage("Parameter 'num' must be a valid integer")
        }
    }
}
